Ext.define('ZYY.controller.system.dept',{
      extend : 'Ext.app.Controller',
      views:['system.deptTree','system.dept','system.deptList','system.deptEdit','system.deptGrid'],
      stores: ['system.deptTree','system.deptGrid'],
      refs : [
     	  {ref: 'dept', selector: '', xtype: 'department',  autoCreate: true},
          {ref: 'deptTree', selector: 'deptTree'},
          {ref: 'deptGrid', selector: 'deptGrid'}
      ],
      init:function(application){
      	
      		if (this.inited) {
					return;
				}
		  this.inited = true;
      	  this.control({
      	  	"department deptTree":{
      	  		render:function(tree){
      	  			tree.expandAll();
      	  		},
      	  		itemclick:this.clickTree
      	  	},
      	  	"department deptTree tool":{
      	  		click:this.reloadTree
      	  	},
      	  	"deptList toolbar button[action=add]":{
      	  		click : this.add
      	  	},
      	  	"deptList toolbar button[action=edit]":{
      	  		click : this.edit
      	  	},
      	  	"deptList toolbar button[action=delete]":{
      	  		click : this.del
      	  	},
      	  	"deptEdit  button[action=save]":{
      	  		click:this.saveDept
      	  	}
      	  	
      	  });
    	  console.log('控制器'+this.$className+'初始化');
      },

	    actionIndex: function() {
	        this.application.setMainView(this.getDept());
	    },
      /*
       * 刷新部门树
       */
      reloadTree:function(button){
	      	var tree =button.up("panel");
			tree.store.getRootNode().removeAll();
			tree.store.load();
			setTimeout(function() {
						tree.expandAll();
					},200);
	  },
	  clickTree:function(view, record){
//	  		if(record.data.leaf){
//	  			this.getSystemDeptGridStore().removeAll();
//	  		}else{
	  			this.getSystemDeptGridStore().getProxy().url=path+'/admin/dept/deptGrid.htm?id='+record.data.id;
      	  		this.getSystemDeptGridStore().load();
//	  		}
	  },
	  add:function(){
		  	var node=this.getDeptTree().getSelectionModel().getSelection()[0];//获取Tree选择节点
		  	var edit = Ext.widget('deptEdit',{title:"新增部门",iconCls:"add"});
		  	if(node){
		  		edit.down('textfield[name=parentdept]').setValue(node.data.text);
		  		edit.down('hiddenfield[name=pid]').setValue(node.data.id);
		  	}
	  },
	  edit:function(button){
	  	   var edit = Ext.widget('deptEdit',{title:"修改部门",iconCls:"edit"});
	  	   var record = button.up('deptList').down('deptGrid').getSelectionModel().getSelection()[0];
	  	   edit.down('textfield[name=parentdept]').setValue(record.data.pname);
	  	   edit.down('textfield[name=deptname]').setValue(record.data.deptname);
	  	   edit.down('textfield[name=sortno]').setValue(record.data.sortno);
		   edit.down('hiddenfield[name=pid]').setValue(record.data.pid);
		   edit.down('hiddenfield[name=deptid]').setValue(record.data.deptid);
	  	   
	  },
	  del:function(){
		  	var records = this.getDeptGrid().getSelectionModel().getSelection();
		  	var store = this.getSystemDeptGridStore();
		    Ext.MessageBox.confirm('提示', '删除部门的同时会删除该部门下面的子部门，确认删除？', function(btn){
		    	if(btn=="yes"){
				  	var ids=[];
				  	for(var i=0;i<records.length;i++){
				  		ids.push(records[i].data.deptid);
				  	}
				  	$.post(path+"/admin/dept/delete.htm",{
				  		ids:ids.toString()
				  	},function(data){
				  		if(data>0){
				  			 Ext.MessageBox.alert('提示', "删除成功！");
				  			 store.remove(records);
				  		}
				  	});
		    	}
		    	
		    });
	  },
	  saveDept:function(button){
	  		var win=button.up("window");
	   		var form = win.down('form').getForm();
	   		var url;
	   		if(form.findField("deptid").getValue()){
	   			 url =  path+"/admin/dept/update.htm";
	   		}else{
	   			 url =  path+"/admin/dept/add.htm";
	   		}
	   		console.log(form.getValues());
            if(form.isValid()){
                form.submit({
                    url:url,
                    success: function(f, o) {
                    	form.reset();
                    	win.close();
                        Ext.MessageBox.alert('提示', "操作成功！");
                    }
                });
            }
	  }
});